package com.henghai.zhenxuan.ui.page.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.LogisticsItem;
import com.henghai.zhenxuan.data.bean.MyHarvestBean;
import com.henghai.zhenxuan.ui.view.LogisticsTrackView;
import com.henghai.zhenxuan.R;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 快递详情
 */
public class FastMailDetailsFragment extends BaseFragment {
    private FastMailDetailsState mState;
    private RefreshLayout mRefreshLayout;

    private static final String TAG = "FastMailDetailsFragment";


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        MyHarvestBean bean = (MyHarvestBean) bundle.getSerializable("MyHarvestBean");
        Log.d(TAG, "onViewCreated: " + bean.toString());
        LogisticsTrackView logisticsView = view.findViewById(com.henghai.zhenxuan.R.id.logistics_view);
        // 准备物流数据
        List<LogisticsItem> items = new ArrayList<>();
        items.add(new LogisticsItem("2023-06-15 14:30:45", "【深圳市】快件已到达 深圳福田集散中心", true));
        items.add(new LogisticsItem("2023-06-15 10:15:22", "【广州市】快件已离开 广州中心\n发往 深圳福田集散中心", false));
        items.add(new LogisticsItem("2023-06-15 08:45:10", "【广州市】顺丰速运 已收件1", false));
        items.add(new LogisticsItem("2023-06-15 08:45:10", "【广州市】顺丰速运 已收件2", false));
        items.add(new LogisticsItem("2023-06-15 08:45:10", "【广州市】顺丰速运 已收件3", false));
        items.add(new LogisticsItem("2023-06-15 08:45:10", "【广州市】顺丰速运 已收件4", false));
        items.add(new LogisticsItem("2023-06-15 08:45:10", "【广州市】顺丰速运 已收件5", false));

        // 设置数据
        logisticsView.setItems(items);

//        // 自定义颜色（可选）
//        logisticsView.setLineColor(Color.parseColor("#E0E0E0"));
//        logisticsView.setPointColor(Color.parseColor("#9E9E9E"));
//        logisticsView.setCurrentPointColor(Color.parseColor("#FF5722"));
//        logisticsView.setTextColor(Color.parseColor("#333333"));
//        logisticsView.setTimeTextColor(Color.parseColor("#999999"));

        // 设置数据
        logisticsView.setItems(items);
        // 设置折叠时显示的条目数量（默认3条）
        logisticsView.setCollapsedItemCount(3);
//        logisticsView.setExpanded(true); // 展开
//        logisticsView.setExpanded(false); // 折叠
//        logisticsView.toggleExpanded(); // 切换状态


        // 设置第一个圆点样式（更大、颜色不同）
        logisticsView.setFirstPointColor(Color.parseColor("#2196F3")); // 蓝色
        logisticsView.setFirstPointRadius(8); // 8dp
        // 设置最后一个圆点样式（颜色不同）
        logisticsView.setLastPointColor(Color.parseColor("#9C27B0")); // 紫色
        logisticsView.setLastPointRadius(6); // 6dp
        // 设置普通圆点大小
        logisticsView.setNormalPointRadius(5); // 5dp
        logisticsView.setPointColor(Color.parseColor("#9E9E9E"));
        // 控制是否使用特殊样式（默认true）
        logisticsView.setUseSpecialFirstPoint(true);
        logisticsView.setUseSpecialLastPoint(true);
        // 设置展开状态变化监听
        logisticsView.setOnExpandStateChangeListener(new LogisticsTrackView.OnExpandStateChangeListener() {
            @Override
            public void onExpandStateChanged(boolean isExpanded) {
                // 处理展开状态变化
                Log.d(TAG, "Is expanded: " + isExpanded);
            }
        });
    }
    @Override
    protected void initViewModel() {
        mState = getFragmentScopeViewModel(FastMailDetailsState.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_fratmaildetails, BR.vm, mState)
                .addBindingParam(BR.click,new ClickProxy());
    }

    public class ClickProxy {
        public void back() {
            nav().popBackStack();
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mState.pageNum = 1;
            mRefreshLayout = refreshLayout;
        }
    }

    public class ListenerLoadHandler implements OnLoadMoreListener {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            if (mState.rows >= Integer.parseInt(mState.PageSize)) {
                mState.pageNum++;

            } else {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    public static class FastMailDetailsState extends StateHolder {
        public int pageNum = 1;
        public int rows;
        public final String PageSize = "20";
        // 准备数据
//        public List<ExpressDetailView.TimelineItem> timelineItems = new ArrayList<>();
    }
}
